package ch.rolandschaer.ascrblr.data;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public abstract class Feed<F extends Feed<?>> {
	
	/** Logger instance */
	public static Logger logger = Logger.getLogger(Feed.class.getName());
	
	/** Web service base URL with trailing slash */
	protected static final String WEBSERVICE_URL = "http://ws.audioscrobbler.com/1.0/";
	
	/** URL Encoding */
	protected static final String ENCODING = "UTF-8";
	
	/** Document builder */
	private DocumentBuilder builder;
	
	/** Parsed document */
	private Document document;
	
	public Feed () throws ParserConfigurationException {
		
		this.builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		
	}
	
	public void parse(InputStream responseStream) throws IOException, SAXException {
		
		Long start = System.currentTimeMillis();
		document = builder.parse(responseStream);
		logger.info("Parsing done in " + (System.currentTimeMillis()-start) + "ms");
		
	}
	
	public Document getDocument() {
		
		return document;
		
	}
	
	public abstract String getUrl() throws UnsupportedEncodingException;
	
}
